home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9997 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  41 lines

  1. Newsgroups: comp.lang.c
  2. Path: vtf.idx.com!news
  3. From: Steve Mount <sjjm@hawkeye.idx.com>
  4. Subject: Empty String function
  5. Message-ID: <1996Mar14.205741.19178@vtf.idx.com>
  6. Sender: news@vtf.idx.com (USENET News System)
  7. Organization: IDX Systems Corporation
  8. Date: Thu, 14 Mar 1996 20:57:41 GMT
  9.  
  10. I benchmarked several methods for testing a buffer's 
  11. emptiness.  Empty is defined as all characters being space,
  12. newline, or tab.  I hit on one very fast method; it
  13. blew the others out of the water.  I presume the standard
  14. function I'm using is optimized.  But it has a potential
  15. problem I'm concerned about - can anyone tell me if it 
  16. could be illegal, or at the least, bad form? 
  17. Here it is:
  18.  
  19. int zemp(char *ptxt, int len)
  20. {
  21. static int i;
  22.  
  23. if (len == 0) len = strlen(ptxt);  // Allow null-term strings
  24. i = strspn(ptxt," \n\t");
  25. if (i >= len) return(-1);          // -1 == EMPTY
  26. return(i);
  27. }
  28.  
  29.  
  30. The problem is when the buffer is not null-terminated, which
  31. happens a lot.  The strspn function COULD read past the end
  32. of the buffer.... potentially WAY past it.  Possibly even into
  33. memory not owned by the program.  This is where I get concerned.
  34. Anyone know if this could cause trouble, generally, and if
  35. it could cause trouble specifically in DOS/Win/Unix (it is
  36. used in a Unix app now).
  37.  
  38. Thanks for input in advance.
  39.  
  40. -Steve
  41.